package net.zhuike.novel.app.ui.activity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.umeng.analytics.MobclickAgent;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.DownloadTaskBean;
import net.zhuike.novel.app.service.DownloadBService;
import net.zhuike.novel.app.ui.adapter.DownLoadAdapter;
import net.zhuike.novel.app.ui.weight.itemdecoration.DefaultItemDecoration;

import butterknife.Bind;
import framework.common.baseui.UeBaseActivity;

/**
 * Created by qi.wang on 2017/6/20.
 *  缓存管理
 */

public class CacheActivity extends UeBaseActivity implements DownloadBService.OnDownloadListener{
    @Bind(R.id.refresh_rv_content)
    RecyclerView mRvContent;
    @Bind(R.id.tips_layout)
    LinearLayout tipsLayout;
    private DownLoadAdapter mDownloadAdapter;
    private ServiceConnection mConn;
    private DownloadBService.IDownloadManager mService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_cache);
        hiddenFooter();
        showGoBackBtn();
        setTitleText("缓存管理");
        setUpAdapter();
    }
    private void setUpAdapter(){
        mDownloadAdapter = new DownLoadAdapter();
        mRvContent.addItemDecoration(new DefaultItemDecoration(this));
        mRvContent.setLayoutManager(new LinearLayoutManager(this));
        mRvContent.setAdapter(mDownloadAdapter);
        processLogic();
        mDownloadAdapter.setOnItemClickListener(
                (view, pos) -> {
                    //传递信息
                    DownloadTaskBean bean = mDownloadAdapter.getItem(pos);
                    switch (bean.getStatus()){
                        //准备暂停
                        case DownloadTaskBean.STATUS_LOADING:
                            mService.setDownloadStatus(bean.getTaskName(),DownloadTaskBean.STATUS_PAUSE);
                            break;
                        //准备暂停
                        case DownloadTaskBean.STATUS_WAIT:
                            mService.setDownloadStatus(bean.getTaskName(),DownloadTaskBean.STATUS_PAUSE);
                            break;
                        //准备启动
                        case DownloadTaskBean.STATUS_PAUSE:
                            mService.setDownloadStatus(bean.getTaskName(),DownloadTaskBean.STATUS_WAIT);
                            break;
                        //准备启动
                        case DownloadTaskBean.STATUS_ERROR:
                            mService.setDownloadStatus(bean.getTaskName(),DownloadTaskBean.STATUS_WAIT);
                            break;
                    }
                }
        );
    }

    public void processLogic() {


        mConn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mService = (DownloadBService.IDownloadManager) service;
                //添加数据到队列中
                mDownloadAdapter.addItems(mService.getDownloadTaskList());

                mService.setOnDownloadListener(CacheActivity.this);
                if(mService.getDownloadTaskList().size()>0){
                    tipsLayout.setVisibility(View.GONE);
                }else{
                    tipsLayout.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                tipsLayout.setVisibility(View.VISIBLE);
            }
        };
        //绑定
        bindService(new Intent(this, DownloadBService.class), mConn, Service.BIND_AUTO_CREATE);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConn);
    }

    @Override
    public void onDownloadChange(int pos, int status, String msg) {
        try {
            DownloadTaskBean bean = mDownloadAdapter.getItem(pos);
            bean.setStatus(status);
            if (DownloadTaskBean.STATUS_LOADING == status) {
                bean.setCurrentChapter(Integer.valueOf(msg));
            }
            mDownloadAdapter.notifyItemChanged(pos);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onDownloadResponse(int pos, int status) {
        try {
            DownloadTaskBean bean = mDownloadAdapter.getItem(pos);
            bean.setStatus(status);
            mDownloadAdapter.notifyItemChanged(pos);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("缓存");
        MobclickAgent.onResume(context);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("缓存");
        MobclickAgent.onPause(context);
    }
}
